package com.pro_one.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pro_one.dao.CheckItemDao;
import com.pro_one.entity.PageResult;
import com.pro_one.entity.QueryPageBean;
import com.pro_one.exception.CheckItemDeleteFailException;
import com.pro_one.pojo.CheckItem;
import com.pro_one.service.CheckItemService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional(readOnly = true)
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;

    /**
     * 添加检查项
     *
     * @param checkItem
     */
    @Override
    @Transactional(readOnly = false)
    public void addCheckItem(CheckItem checkItem) {
        checkItemDao.addCheckItem(checkItem);
    }

    /**
     * 列表查询以及根据条件查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findByCondition(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        List<CheckItem> all = checkItemDao.findByCondition(queryString);
        PageInfo pageInfo = new PageInfo(all);

        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 根据id查询检查项
     *
     * @param id
     * @return
     */
    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }

    /**
     * 修改检查项
     *
     * @param checkItem
     */
    @Override
    @Transactional(readOnly = false)
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    /**
     * 删除检查项
     *
     * @param id
     */
    @Override
    @Transactional(readOnly = false)
    public void delete(Integer id) {
        int count = checkItemDao.findCheckGroupById(id);
        if (count > 0) {
            throw new CheckItemDeleteFailException();
        }
        checkItemDao.deleteById(id);
    }


}
